/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.ihm;

import ctrl.jeu.Place;
import java.awt.event.ActionEvent;
import mod.dao.membre;
import mod.dao.tables;
import java.awt.Graphics;
import java.sql.ResultSet;
import javax.swing.JPanel;
import ctrl.sys.ClientReceive;
import ctrl.sys.ClientSend;
import ctrl.sys.Const;
import ctrl.sys.Demande;
import ctrl.jeu.Table;
import ctrl.sys.clientTCP;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
import view.com.PgButton;


/**
 *
 * @author Willy
 */
public class Load extends JPanel {

    Table t;
    clientTCP clientReseau;
    
    JPanel Tchat;
    JPanel Suggestion;
    JApplet High;
    membre Prs;

    /** Creates new form Test */
    public Load(String ser, int idTable, membre pers, JApplet par) {

        clientReseau=new clientTCP();
        clientReseau.Connexion(ser);

        High=par;
        Prs=pers;

        PgButton ret=new PgButton("Lobby");
        setSize(800,600);
        //setTitle("P. G: Table de jeu");
        setLayout(null);
        //setBackground(Const.couleur);
        add(ret); ret.setBounds(10,10,100,30);
        ret.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Arret();
                High.setSize(600,400);
                High.add(new guiTable(Prs,High));
                High.remove(getThis());
                High.repaint();
            }
        });

        
        //GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        // Maximise l'ecran à l'ouverture
    	//this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
        //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // lecture des tables en bd et création des instances
        tables tabledao=new tables();
        ResultSet r=tabledao.Liste(idTable);
        try {
            if (r.next())
            {
                t=(new Table(r.getInt("id"),r.getInt("blind"),r.getInt("min"),r.getInt("max")));
                JLabel lb=new JLabel(new ImageIcon(t.trans));
                lb.setBounds(100,30,849,436);
                this.add(lb);

                //break;
            }
        }catch(Exception e)
        {

        }
        // initialisation des panels
        panTexteAction TexteAction=new  panTexteAction();
        TexteAction.setBounds(20,530, 300, 200);
        t.setTexteAction(TexteAction);
        add(TexteAction);


        new ClientReceive(clientReseau.getClient(),t).start();
        t.Play.setNom(pers.getNOM());
        t.Play.setPrenom(pers.getPRENOM());
        t.Play.setMontantTotal(pers.getMONTANT());
        t.Play.setPhoto(pers.getPHOTO());

        //repaint();
    }

    public JPanel getThis()
    {
        return this;
    }
    public void Entree()
    {
        t.Affiche(this,100,100);
        // mettre un affiche avant, car il charge tout d'abord la fentre contenue dans sur la table de jeu
        Demande as=new Demande(Const.ENTER,0,t.getId(),-1);
        new ClientSend(clientReseau.getClient(), as).start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        
        t.Affiche(this,100,100);
        validate();
    }

    public void Arret()
    {
        //avant de sortir de cette fenetre on envoi un message au serveur disant qu'on veut se déconnecter
            Place PlCh=t.getPlaceMain();
            int IdCh=-1;
            if(PlCh!=null) IdCh=PlCh.getIdChaise();

            Demande as=new Demande(Const.QUIT,0,t.getId(),IdCh);
            new ClientSend(clientReseau.getClient(), as).start();
    }
    
    public static void main(String[] args)
    {
        Load t=null;
        if(args.length>0)
        {
            //t=new Load(args[0],1);
        }
        else
        {
            //t=new Load(config.serveurSocket,1);
        }
        t.setVisible(true);
        //t.repaint();
        
    }

}